import categories from './categories.js'
import orders from './orders.js'
import storeOrders from './storeOrders.js'
import boardcast from './boardcast.js'
import mart from './mart.js'
import productList from './productList.js'
import notices from './notices.js'
import productDetail from './productDetail.js'
import scores from './scores.js'
import martDetail from './martDetail.js'
import hotSearch from './hotSearch.js'
import historySearch from './historySearch.js'
import orderDetail from './orderDetail.js'
import giftCards from './giftCards.js'
import { baseUrl } from "../config"
const json = {
	categories,
	orders,
	storeOrders,
	boardcast,
	mart,
	productList,
	notices,
	productDetail,
	scores,
	martDetail,
	hotSearch,
	historySearch,
	orderDetail,
	giftCards
}

function baseUrlFunc(url) {
	return `${baseUrl}${url}`;
}
const ajax = function(opt) {
	opt = opt || {};
	if (opt.url.indexOf('http') == -1) {
		opt.url = baseUrlFunc(opt.url) || '';
	}
	opt.data = opt.data || null;
	opt.method = opt.method || 'GET';
	opt.header = opt.header || {
		// 'Authorization': ComFunJS.token || '',
		'content-type': 'application/json',
		'Accept': 'application/json'
	};
	return new Promise((resolve, reject) => {
		uni.request({
			url: opt.url,
			data: opt.data,
			method: opt.method,
			header: opt.header,
			dataType: 'json',
			success: function(res) {
				resolve(res.data);
			},
			fail: function(res) {
				console.log(res);
				reject(res);
			},
			complete(res) {
				console.log(res);
				if (res.data.status != 1000) {
					if (res.data.status === undefined) {
						// 请求错误
						// _this.logout();
						// // 回到登录页
						// uni.reLaunch({
						// 	url: '/pages/login/index'
						// });
						// return
					}
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 1000
					});
				}
			}
		})
	});
	
}
const list = ["categories"];
export default (name, loading = true) => {
	if (loading) {
		uni.showLoading()
	}

	return new Promise(resolve => {
		setTimeout(() => {
			uni.hideLoading()
			if (list.indexOf(name) > -1) {
				// console.log(json[name]);
				ajax(json[name]).then(res => {
					resolve(res);
				}).catch(e => {});
			} else {
				resolve(json[name])
			}


		}, 500)
	})
}
